<?php
// $Id$
/**
 * @file featuresreport.database.inc
 * 
 * This file gathers all 'hook_elements_in_database()', invoked by featuresreport,
 * where "hook" is a COMPONENT name like content, fieldgrou)p, views, variable etc...
 * 
 * Those hooks return an array of all elements living ONLY in database for the given component.
 * This array is formated the way elements informations are formated in a features' .info
 * 
 * To extend this module, all we have to do is to add a new hook in this file for a new component
 * ex : menu_elements_in_database(), wich is still missing for now :-p
 * 
 */
 
/**
 * Implements hook_elements_in_database() for context
 * @see context.module
 */
function context_elements_in_database() {
  
  $version = featuresreport_get_module_version('context');
  $elements_in_database = array();
  
  switch ($version) {
    
    case '2' :
      
      $contexts = context_contexts();
      foreach ($contexts as $name => $context) {
        // we don't want default and overriden contexts
        if ($context->type == CONTEXT_STORAGE_NORMAL) {
          $elements_in_database[] = $name;
        }
      }
      return $elements_in_database;
      
    case '3' :
      
      // exportables of context 3 are provided by ctools
      // context_load is some kind of wrapper for ctools functions 
      $contexts = context_load();
      foreach ($contexts as $name => $context) {
        // we don't want default and overriden contexts
        if ($context->type == 'Normal') {
          $elements_in_database[] = $name;
        }
      }
        
      return $elements_in_database;  
      
  }
  
}

/**
 * Implements hook_elements_in_database() for drupal variables
 * 
 * Strongarm use CTOOLS with variable table.
 * @see strongarm.module
 * @see variable table in database
 */
function variable_elements_in_database() {
  $objects_in_database = array();
  ctools_include('export');
  $objects = ctools_export_load_object('variable');
  foreach ($objects as $object) {
    if ($object->type == 'Normal') {
      $objects_in_database[] = $object->name;
    }
  }
  return $objects_in_database;
}

/**
 * Implements hook_elements_in_database() for panels
 * 
 * Panels use Ctools with page_manager_pages table
 * @see panels.module
 * @todo see if other modules are using page_manager_pages
 */
function page_manager_pages_elements_in_database() {
  
  $version = featuresreport_get_module_version('panels');
  if ($version < 3) {
    $message = t('It seems that you have not installed panels 3, but a previous version of panels. Features report does not support panels 2 and previous versions.');
    drupal_set_message($message, 'error');
    return;
  }
  
  if ($version >= 3) {
    $objects_in_database = array();
    ctools_include('export');
    $objects = ctools_export_load_object('page_manager_pages');
    foreach ($objects as $object) {
      if ($object->type == 'Normal') {
        $objects_in_database[] = $object->name;
      }
    }
    return $objects_in_database;
  }

  $elements_in_database = array();
  
}
  
/**
 * Implements hook_elements_in_database() for cck fields
 * 
 * Return all cck fields. fields can only live in database anyway.
 * @see content.module
 */
function content_elements_in_database() {
  $elements_in_base = array();
  foreach (content_types() as $type_name => $datas) {
    foreach ($datas['fields'] as $field_name => $datas) {
      // dans les .infos des features, les champs cck contiennent à la fois le nom du champ et du type de contenu
      // on formate notre tableau de la même façon pour faire le diff facilement.
      $elements_in_base[] = $type_name . '-' . $field_name;
    }      
  }
  return $elements_in_base;
}
  
/**
 * implements hook_elements_in_database() for imagecache
 * 
 * @see imagecache.module
 */
function imagecache_elements_in_database() {
  $elements_in_database = array();
  foreach (imagecache_presets() as $preset) {
    if ($preset['storage'] == IMAGECACHE_STORAGE_NORMAL) {
      $elements_in_database[] = $preset['presetname'];
    }
  }
  return $elements_in_database;
}
  
/**
 * implements hook_elements_in_database() for fieldgroups
 * @see fieldgroup.module
 */
function fieldgroup_elements_in_database() {
  $elements_in_database = array();
  foreach (content_types() as $content_type => $datas) {
    foreach (fieldgroup_groups($content_type) as $fieldgroup_name => $datas) {
      $elements_in_database[] = $content_type . '-' . $fieldgroup_name;
    }
  }
  return $elements_in_database;
}
  
/**
 * implements hook_elements_in_database() for views
 * @see views.module
 */
function views_elements_in_database($reset = FALSE) {
  $elements_in_database = array();
  if ($views = views_get_all_views()) {
    foreach ($views as $view)  {
      if ($view->type == 'Normal') {
        $elements_in_database[] = $view->name;
      }
    }
  }
  return $elements_in_database;
}

/**
 * Implements hook_elements_in_database() for nodes
 *
 * Features seems too only add to features.info in "node" elements
 * content types that are created via the admin of content types.
 * they are tagged as "node" in database (in "module" column).
 * @todo check this...
 * 
 * @see node.module
 */
function node_elements_in_database() {
  $elements_in_database = array();  
  $type_result = db_query("SELECT type FROM {node_type} WHERE module = 'node'");
  while ($type_object = db_fetch_object($type_result)) {
    $elements_in_database[] = $type_object->type;
  }
  return $elements_in_database;
}
